package com.koudai.reader;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.iapppay.mpay.ifmgr.SDKApi;
import com.iapppay.pay.mobile.iapppaysecservice.utils.IAppPaySDKConfig;
import com.koudai.reader.android.R;
import com.koudai.reader.conponment.ActiveListener;
import com.koudai.reader.conponment.GotoSearchDialog;
import com.koudai.reader.db.SingleDAO;
import com.koudai.reader.util.HttpUtils;
import com.koudai.reader.util.Utils;
import com.koudai.reader.vo.EndUser;
import com.koudai.reader.vo.ReadBookConfig;
import com.koudai.reader.vo.Task;
import com.umeng.analytics.MobclickAgent;

public class MainTabActivity extends TabActivity {

    /** Called when the activity is first created. */
    protected void onResume() {
        super.onResume();

        
        MobclickAgent.onResume(this);
        EndUser.getInstance().init(this);
        SingleDAO.getinstance().init(this);
        ReadBookConfig.getInstance().init(this);
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    
                    List<Task> list = HttpUtils.taskList();

                    StringBuilder result = new StringBuilder();
                    result.append("您提交的搜索请求:<br/><br/>");
                    boolean flg = false;
                    List<String> matchKeyword = new ArrayList<String>();
                    for (Task item : list) {
                        if (item.getStatus().equals("2")) {
                            flg = true;
                            result.append("     <font color='blue'>\"").append(item.getContent()).append("\"</font><br/>");
                            matchKeyword.add(item.getContent());
                        }
                    }

                    String penddingkeyword = "";
                    if (matchKeyword.size() == 1) {
                        penddingkeyword = matchKeyword.get(0);
                    }

                    result.append("<br/>书海小助手已经为您找到了");
                    if (flg) {
                        final String tmp = result.toString();
                        final String tmp2 = penddingkeyword;
                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                new GotoSearchDialog(MainTabActivity.this, tmp, tmp2).show();
                            }
                        });
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

        SDKApi.init(this, SDKApi.PORTRAIT, IAppPaySDKConfig.APP_ID);

        
    };

    private Handler handler = new Handler();
    private com.koudai.reader.conponment.ActiveListener activeListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main_tab);
        EndUser.getInstance().init(this);
        SingleDAO.getinstance().init(this);
        
        activeListener = new ActiveListener();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.PACKAGE_ADDED");
        filter.addDataScheme("package");
        registerReceiver(activeListener, filter);
        
        final TabHost tabs = getTabHost();
        final TabWidget tabWidget = tabs.getTabWidget();

        Field mBottomLeftStrip;
        Field mBottomRightStrip;

        
        
        tabs.addTab(tabs.newTabSpec("").setIndicator("我的书架", null)
                .setContent(new Intent(MainTabActivity.this, BookShelfActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        tabs.addTab(tabs.newTabSpec("书海").setIndicator("排行榜", null)
                .setContent(new Intent(MainTabActivity.this, BookCityActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        tabs.addTab(tabs.newTabSpec("分类").setIndicator("书海", null)
                .setContent(new Intent(MainTabActivity.this, CateListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        tabs.addTab(tabs.newTabSpec("更多").setIndicator("更多", null)
                .setContent(new Intent(MainTabActivity.this, SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            /**
             */
            tabWidget.getChildAt(i).getLayoutParams().height = Utils.convertToScreenPixels(50);

            final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0); // 取消文字底边对齐
            params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            /**

             */
            if (Build.VERSION.SDK_INT <= 7) {
                try {
                    mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mBottomLeftStrip");
                    mBottomRightStrip = tabWidget.getClass().getDeclaredField("mBottomRightStrip");
                    if (!mBottomLeftStrip.isAccessible()) {
                        mBottomLeftStrip.setAccessible(true);
                    }
                    if (!mBottomRightStrip.isAccessible()) {
                        mBottomRightStrip.setAccessible(true);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {

            }
            View vvv = tabWidget.getChildAt(i);
            if (tabs.getCurrentTab() == i) {
                switch (i) {
                case 0:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                    break;
                case 1:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                    break;
                case 2:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                    break;
                case 3:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                    break;
                }

            } else {
                switch (i) {
                case 0:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                    break;
                case 1:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                    break;
                case 2:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                    break;
                case 3:
                    vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                    break;

                }
            }

        }

        tabs.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub
                for (int i = 0; i < tabWidget.getChildCount(); i++) {
                    View vvv = tabWidget.getChildAt(i);
                    if (tabs.getCurrentTab() == i) {
                        switch (i) {
                        case 0:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                            break;
                        case 1:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                            break;
                        case 2:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                            break;
                        case 3:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon2));
                            break;

                        }

                    } else {
                        switch (i) {
                        case 0:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                            break;
                        case 1:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                            break;
                        case 2:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                            break;
                        case 3:
                            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_icon));
                            break;

                        }
                    }
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        
        super.onDestroy();
        
        if (activeListener != null) {
            unregisterReceiver(activeListener);
        }
    }
}
